--[[	$Id: caelPetCare.lua 3143 2013-04-20 10:13:19Z sdkyron@gmail.com $	]]

local CALL_PET = GetSpellInfo(883) -- changed later
local DISMISS_PET = GetSpellInfo(2641)
local MEND_PET = GetSpellInfo(136)
local REVIVE_PET = GetSpellInfo(982)

local combat, dead, mending, pet, wounded

local mendThreshold = 0.75 -- Mend your pet out of combat by default when its health is below this percent
local mendModifier = "shift" -- Use this modifier key to mend your pet out of combat, or revive it in combat if it's despawned
local dismissModifier = "ctrl" -- Use this modifier key to dismiss your pet

caelPetCare = CreateFrame("Frame")
caelPetCare:SetScript("OnEvent", function(self, event, ...) if self[event] then return self[event](self, ...) end end)
caelPetCare:RegisterEvent("ADDON_LOADED")

function caelPetCare:ADDON_LOADED(addon)
	if addon ~= "caelPetCare" then return end

	if caelLib.playerClass ~= "HUNTER" then
		self:UnregisterAllEvents()
		print("|cffD7BEA5cael|rcaelPetCare: You are not a Hunter, caelPetCare will be disabled on next UI reload.")
		return DisableAddOn("caelPetCare")
	end

	self:UnregisterEvent("ADDON_LOADED")
	self.ADDON_LOADED = nil

	if IsLoggedIn() then
		self:PLAYER_LOGIN(true)
	else
		self:RegisterEvent("PLAYER_LOGIN")
	end
end

function caelPetCare:PLAYER_LOGIN(delayed)

	self:RegisterEvent("PLAYER_ALIVE")
	self:RegisterEvent("PLAYER_REGEN_DISABLED")
	self:RegisterEvent("PLAYER_REGEN_ENABLED")
	self:RegisterEvent("PLAYER_UNGHOST")
	self:RegisterEvent("UI_ERROR_MESSAGE")
	self:RegisterEvent("UNIT_HEALTH")
	self:RegisterEvent("UNIT_PET")
	self:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")

	if not InCombatLockdown() then
		self:RegisterEvent("UNIT_AURA")
	end

	if delayed then
		self:UNIT_PET("player")
	end

	self:UnregisterEvent("PLAYER_LOGIN")
	self.PLAYER_LOGIN = nil
end

function caelPetCare:PLAYER_ALIVE()
	if UnitIsGhost("player") then return end

	self:Edit()
end

caelPetCare.PLAYER_UNGHOST = caelPetCare.PLAYER_ALIVE

function caelPetCare:PLAYER_REGEN_DISABLED()

	self:UnregisterEvent("UNIT_AURA")

	combat = true
	self:Edit()
end

function caelPetCare:PLAYER_REGEN_ENABLED()

	self:RegisterEvent("UNIT_AURA")

	combat = false
	self:UNIT_AURA("pet")
	self:UNIT_HEALTH("pet")
	self:Edit()
end

local ERR_PET_SPELL_NOTDEAD = PETTAME_NOTDEAD.."."
function caelPetCare:UI_ERROR_MESSAGE(message)

	if message == ERR_PET_SPELL_DEAD then
		dead = true
		self:Edit()
	elseif message == ERR_PET_SPELL_NOTDEAD then
		dead = false
		self:Edit()
	end
end

function caelPetCare:UNIT_AURA(unit)
	if unit ~= "pet" then return end

	local wasMending = mending
	mending = nil

	mending = select(7, UnitBuff("pet", MEND_PET))

	if wasMending and not mending then
		self:UNIT_HEALTH("pet")
		self:Edit()
	end
end

function caelPetCare:UNIT_HEALTH(unit)
	if unit ~= "pet" then return end

	local hp, maxhp = UnitHealth("pet"), UnitHealthMax("pet")
	if not dead and hp <= 0 and UnitIsDead("pet") then
		dead = true
		self:Edit()
	elseif dead and hp > 0 then
		dead = false
		self:Edit()
	elseif not wounded and hp / maxhp <= mendThreshold then
		wounded = true
		self:Edit()
	elseif wounded and hp / maxhp > mendThreshold then
		wounded = false
		self:Edit()
	end
end

local callIDs = { [833] = true, [83242] = true, [83243] = true, [83244] = true, [83245] = true }

function caelPetCare:UNIT_SPELLCAST_SUCCEEDED(unit, spell, spellID)
	if callIDs[spellID] then
		CALL_PET = spell
	end
end

function caelPetCare:Edit()
	if InCombatLockdown() then return end

	local macroID = GetMacroIndexByName("AutoPet")
	if not macroID then
		return
	end

	local body = "#showtooltip"
	if UnitAffectingCombat("player") then
		body = body.."\n/cast [target=pet,dead][nopet,mod:"..mendModifier.."] "..REVIVE_PET.."; [nopet] "..CALL_PET.."; [mod:"..dismissModifier.."] "..DISMISS_PET.."; "..MEND_PET
	elseif dead then
		body = body.."\n/cast "..REVIVE_PET
	elseif not pet or not select(2, HasPetUI()) then
		body = body.."\n/cast [target=pet,dead][mod:"..mendModifier.."] "..REVIVE_PET.."; "..CALL_PET
	elseif wounded and not (mending and (GetTime() - mending < 0)) then
		body = body.."\n/cast [mod:"..dismissModifier.."] "..DISMISS_PET.."; "..MEND_PET
	else
		body = body.."\n/cast [mod:"..dismissModifier.."] "..DISMISS_PET.."; [mod:"..mendModifier.."] "..MEND_PET.."; "..FEED_PET
	end

	EditMacro(macroID, "AutoPet", "INV_MISC_QUESTIONMARK", body, 1)
end